//
//  HJApiService.swift
//  SwfitBase
//
//  Created by rubick on 2023/1/17.
//

import Foundation
import Moya

/// 请求Targets

/**
  *  此ApiService仅作参考使用
  *  建议每个模块都单独维护自己的ApiService
  */

enum HJApiServices {
    case homeChannel
    case homeInfo(channelId: Int)
    case testInfo
}
extension HJApiServices: TargetType {
    var baseURL: URL {
        return URL(string: "https://esign.com/")!
    }
    var path: String {
        switch self {
        case .homeChannel:
            return "homeChannel/list"
        case .homeInfo(let channelId):
            return "homeChannel/\(channelId)"
        default:
            return ""
        }
    }
    var method: Moya.Method {
        switch self {
        case .homeChannel:
            return .get
        case .homeInfo:
            return .post
        default:
            return .get
        }
    }
    var task: Moya.Task {
        switch self {
        case .homeChannel:
            let params = ["gender":1,"generation":1]
            return .requestParameters(parameters: params, encoding: URLEncoding.queryString)
        case .homeInfo(let channelId):
            let params = ["gender":1,"generation":1,"limit":20,"offset":0, "channelId": channelId]
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        default:
            return .requestPlain
        }
    }
    var headers: [String : String]? {
        return ["xxx": "xxx"]
    }
    // 单元测试 模拟的数据
    var sampleData: Data {
        return "{}".data(using: .utf8)!
    }
}
// 请求示例
fileprivate func getHomeChannelInfo(channelId: Int) {
    HJNetworkRequest(HJApiServices.homeInfo(channelId: channelId)) { result in
        print(result)
    } failed: { errorMsg, hjError in
        print(errorMsg)
    }
}

